package com.sanjiahantang.youjiaclient.module.goods.adapters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.StaticValue;
import com.sanjiahantang.youjiaclient.module.common.holders.CommonHolder;
import com.sanjiahantang.youjiaclient.module.common.views.impl.MainActivity;
import com.sanjiahantang.youjiaclient.module.goods.beans.GoodsBean;
import com.sanjiahantang.youjiaclient.module.goods.presenters.GoodsPresenter;
import com.sanjiahantang.youjiaclient.module.match.beans.MatchBean;

import java.util.List;


/**
 * Created by zy on 2017/8/22.
 */

public class GoodsInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements StaticValue{

    private LayoutInflater inflater;
    private GoodsBean.Data data;
    private Activity activity;
    private GoodsPresenter presenter;
    private String goodstype;
    private int d10;
    private int pageNo = 1;
    private final static int PAGE_SIZE = 10;

    private final int TYPE_LENGTH = 7;//类型数量
    private CommonHolder recommendHolder = null;
    private View.OnClickListener paramOnClickLister;

    public void setCartNum(int cartNum) {
        this.cartNum = cartNum;
        notifyDataSetChanged();
    }

    public void setParamOnClickLister(View.OnClickListener paramOnClickLister) {
        this.paramOnClickLister = paramOnClickLister;
    }

    private int cartNum = 0;//购物车数量

    public GoodsInfoAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
    }

    public GoodsInfoAdapter(GoodsBean.Data data, Activity activity, GoodsPresenter presenter) {
        this(activity);
        this.data = data;
        this.activity = activity;
        this.presenter = presenter;
        d10 = Utils.dip2px(activity,10);
    }
    public  void  setGoodsType(String type){
              this.goodstype=type;
    }



    @Override
    public int getItemViewType(int position) {
        return HOLDER_TYPE_GOODS_BANNER + position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        switch (viewType) {
            case HOLDER_TYPE_GOODS_BANNER:   //轮播图和输入框
                Object[] param = new Object[]{
                        data.getImgUrlList(),
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(activity,MainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                ConfigValue.hasRegisterReceiver = false;
                                intent.putExtra(KEY_PAGE,3);
                                activity.startActivity(intent);
                            }
                        },
                        cartNum,goodstype
                };
                return new CommonHolder(activity,inflater.inflate(R.layout.item_holder_goods_banner,null),HOLDER_TYPE_GOODS_BANNER,param);
            case HOLDER_TYPE_GOODS_DETAIL:
                Object[] detailParam = new Object[2];
                detailParam[0] = data;
                detailParam[1] = paramOnClickLister;
                return new CommonHolder(activity,inflater.inflate(R.layout.item_holder_goods_detail,null),HOLDER_TYPE_GOODS_DETAIL,detailParam);
            case HOLDER_TYPE_GOODS_COMMENTS:
                return new CommonHolder(activity,inflater.inflate(R.layout.item_holder_goods_comment,null), HOLDER_TYPE_GOODS_COMMENTS,data.getEvaluateList());
            case HOLDER_TYPE_GOODS_WEBVIEW_TITLE:
                return new CommonHolder(activity,inflater.inflate(R.layout.item_holder_goods_web,null), HOLDER_TYPE_GOODS_WEBVIEW_TITLE,null);
            case HOLDER_TYPE_GOODS_WEBVIEW:
                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                WebView mView = new WebView(activity);
                mView.setLayoutParams(lp);
                return new CommonHolder(activity,mView, HOLDER_TYPE_GOODS_WEBVIEW,data.getDetailMemo());
            case HOLDER_TYPE_GOODS_RECOMMEND:
                  View v= inflater.inflate(R.layout.item_holder_goods_recommend,null);
                     v.setTag(R.id.decode,goodstype);
                pageNo = 1;
                return recommendHolder = new CommonHolder(activity,v, HOLDER_TYPE_GOODS_RECOMMEND,data.getGoodsRecommendList().getList());
            case HOLDER_TYPE_GOODS_BOTTOM:
                TextView tv = new TextView(activity);
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tv.setLayoutParams(params);
                tv.setGravity(Gravity.CENTER);
                tv.setPadding(d10,d10,d10,d10);
                tv.setText("没有更多了");
                return new CommonHolder(activity,tv,HOLDER_TYPE_GOODS_BOTTOM,null);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        switch (getItemViewType(position)) {
            case HOLDER_TYPE_GOODS_BANNER:   //轮播图和输入框
                Object[] param = new Object[]{
                        data.getImgUrlList(),
                        new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(activity,MainActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                ConfigValue.hasRegisterReceiver = false;
                                intent.putExtra(KEY_PAGE,3);
                                activity.startActivity(intent);
                            }
                        },
                        cartNum,goodstype
                };
                ((CommonHolder)holder).bindData(activity,getItemViewType(position),param);
                break;
            case HOLDER_TYPE_GOODS_DETAIL:
                Object[] detailParam = new Object[2];
                detailParam[0] = data;
                detailParam[1] = paramOnClickLister;
                ((CommonHolder)holder).bindData(activity,getItemViewType(position),detailParam);
                break;
            case HOLDER_TYPE_GOODS_COMMENTS:
                ((CommonHolder)holder).bindData(activity,getItemViewType(position),data.getEvaluateList());
                break;
            case HOLDER_TYPE_GOODS_WEBVIEW_TITLE:
                ((CommonHolder)holder).bindData(activity,getItemViewType(position),null);
                break;
            case HOLDER_TYPE_GOODS_WEBVIEW:
                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                WebView mView = new WebView(activity);
                mView.setLayoutParams(lp);
                ((CommonHolder)holder).bindData(activity,getItemViewType(position),data.getDetailMemo());
                break;
            case HOLDER_TYPE_GOODS_RECOMMEND:
//                recommendHolder.bindData(activity,getItemViewType(position) ,data.getGoodsRecommendList().getList());
                break;
        }

    }

    @NonNull
    private List<MatchBean.Goods> getCurrent(){
        List<MatchBean.Goods> list = data.getGoodsRecommendList().getList();
        if (list.size() > (pageNo + 1) * PAGE_SIZE){
            return list.subList(pageNo * PAGE_SIZE,(++pageNo) * PAGE_SIZE);
        }else{
            return list.subList(pageNo,list.size());
        }
    }

    @Override
    public int getItemCount() {
        return TYPE_LENGTH;
    }

    public void loadMore(){
        recommendHolder.bindData(activity,HOLDER_TYPE_GOODS_RECOMMEND + (pageNo == 0 ? 0 : 0x60),getCurrent());
    }


    public void loadMore(@NonNull List<MatchBean.Goods> list){
        pageNo++;
        recommendHolder.bindData(activity,HOLDER_TYPE_GOODS_RECOMMEND_MORE,list);
    }

    public int getPageNo(){
        return pageNo;
    }
}
